package z.explorer.ui.component.walker;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import z.explorer.Main;
import z.explorer.core.storage.ExecutionDetail;
import z.explorer.core.storage.Operation;
import z.explorer.core.storage.User;
import z.explorer.core.storage.dao.ExecutionDetailDAO;
import z.explorer.core.storage.dao.OperationDAO;
import z.explorer.core.storage.dao.ParamDAO;
import z.explorer.ui.common.Handler;
import z.explorer.ui.common.ZTable;
import z.explorer.ui.common.ZTablePanel;
import z.explorer.ui.component.login.Login;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.*;

public class Configuration extends JPanel implements Handler {

	private ZTablePanel operationTable = new ZTablePanel(null);

	private ParamTable paramTable = new ParamTable();

	private ZTablePanel executionTable = new ZTablePanel(null);

	private JButton actionButton = new JButton("Add");

	private JButton removeButton = new JButton("Remove");

	private JButton upButton = new JButton("  Up  ");

	private JButton downButton = new JButton(" Down ");

	private JButton previewButton = new JButton("Preview");

	private JTextField runDateField = new JTextField(20);

	private JTextArea previewCode = new JTextArea(3, 80);

	private int table_id = -1;
	private int exe_id = -1;
	private int op_id = -1;

	public Configuration() {
		initUI();
		initEvent();
		loadData();
	}

	public void initEvent() {
		ZTable zt = operationTable.getZTable();
		zt.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						int rowIndex = operationTable.getZTable()
								.getSelectedRow();
						int columnIndex = operationTable.getZTable()
								.getzTableModel().getIndexByName("precode");
						if (rowIndex == -1) {
							actionButton.setEnabled(false);
							return;
						}
						actionButton.setEnabled(true);
						actionButton.setActionCommand("Add");
						actionButton.setText("Add");
						executionTable.getZTable().getSelectionModel()
								.clearSelection();
						String precode = (String) operationTable.getZTable()
								.getValueAt(rowIndex, columnIndex);
						paramTable.loadData(precode);
						columnIndex = operationTable.getZTable()
								.getzTableModel().getIndexByName("op_id");
						op_id = (Integer) operationTable.getZTable()
								.getValueAt(rowIndex, columnIndex);

					}
				});
		zt = executionTable.getZTable();
		zt.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {

						int rowIndex = executionTable.getZTable()
								.getSelectedRow();
						int columnIndex = executionTable.getZTable()
								.getzTableModel().getIndexByName("exe_seq");
						if (rowIndex == -1) {
							actionButton.setEnabled(false);
							removeButton.setEnabled(false);
							upButton.setEnabled(false);
							downButton.setEnabled(false);
							return;
						}
						actionButton.setEnabled(true);
						removeButton.setEnabled(true);
						upButton.setEnabled(true);
						downButton.setEnabled(true);
						actionButton.setActionCommand("Update");
						actionButton.setText("Update");
						operationTable.getZTable().getSelectionModel()
								.clearSelection();
						Integer exe_seq = (Integer) executionTable.getZTable()
								.getValueAt(rowIndex, columnIndex);
						paramTable.loadData(table_id, exe_seq);
						exe_id = exe_seq;
					}

				});

		actionButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				User user = (User) Main.context.getBean("user");
				if (user.getName().equals("undefined")) {
					JOptionPane
							.showMessageDialog((JFrame) Main.context
									.getBean("zExplorer"),
									"you have not logined in!!!");
					Login login = new Login(Login.LOGIN);
					return;
				}
				if (actionButton.getActionCommand().equals("Add")) {
					ParamDAO pdao = (ParamDAO) Main.context.getBean("paramDAO");

					pdao.insertParam(paramTable.getParamList(), table_id,
							op_id, user.getName());

					JOptionPane.showMessageDialog((JFrame) Main.context
							.getBean("zExplorer"),
							"you have added the execution!");
					loadExecution();
				} else if (actionButton.getActionCommand().equals("Update")) {
					ParamDAO pdao = (ParamDAO) Main.context.getBean("paramDAO");
					pdao.updateParam(paramTable.getParamList(), table_id,
							exe_id, user.getName());
					JOptionPane.showMessageDialog((JFrame) Main.context
							.getBean("zExplorer"),
							"you have updated the execution!");
				}
			}
		});

		removeButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				User user = (User) Main.context.getBean("user");
				if (user.getName().equals("undefined")) {
					JOptionPane
							.showMessageDialog((JFrame) Main.context
									.getBean("zExplorer"),
									"you have not logined in!!!");
					Login login = new Login(Login.LOGIN);
					return;
				}
				ParamDAO pdao = (ParamDAO) Main.context.getBean("paramDAO");
				pdao.removeExeSeq(table_id, exe_id,user.getName());
				exe_id=-1;
				paramTable.loadData(table_id, exe_id);
				loadExecution();
			}

		});
		
		upButton.addActionListener(new ActionListener (){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				User user = (User) Main.context.getBean("user");
				if (user.getName().equals("undefined")) {
					JOptionPane
							.showMessageDialog((JFrame) Main.context
									.getBean("zExplorer"),
									"you have not logined in!!!");
					Login login = new Login(Login.LOGIN);
					return;
				}
				ParamDAO pdao = (ParamDAO) Main.context.getBean("paramDAO");
				pdao.moveExeSeq(table_id, exe_id,-1,user.getName());
				exe_id=(exe_id==1?1:exe_id-1);
				int sindx=executionTable.getZTable().getSelectedRow();
				if(sindx == 0)
					return;
				loadExecution();
				executionTable.getZTable().setRowSelectionInterval(sindx-1, sindx-1);
			
			}
			
		});
		
		downButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				User user = (User) Main.context.getBean("user");
				if (user.getName().equals("undefined")) {
					JOptionPane
							.showMessageDialog((JFrame) Main.context
									.getBean("zExplorer"),
									"you have not logined in!!!");
					Login login = new Login(Login.LOGIN);
					return;
				}
				ParamDAO pdao = (ParamDAO) Main.context.getBean("paramDAO");
				pdao.moveExeSeq(table_id, exe_id,1,user.getName());
				int rcount=executionTable.getZTable().getRowCount();
				exe_id=(exe_id==rcount?rcount:exe_id+1);
				int sindx=executionTable.getZTable().getSelectedRow();
				if(sindx == rcount-1)
					return;
				loadExecution();
				executionTable.getZTable().setRowSelectionInterval(sindx+1, sindx+1);
				
			}});
		
		previewButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ParamDAO pdao = (ParamDAO) Main.context.getBean("paramDAO");
				int col =executionTable.getZTable().getRowCount();
				String code=pdao.getCode(table_id, col, runDateField.getText());
				previewCode.setText(code);
			}});
	}

	public void initUI() {
		actionButton.setEnabled(false);
		removeButton.setEnabled(false);
		upButton.setEnabled(false);
		downButton.setEnabled(false);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		runDateField.setText(format.format(date));
		previewCode.setEditable(false);
		previewCode.setBackground(Color.RED);
		
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 3;
		gbc.gridheight = 7;
		gbc.weightx = 0.4;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		topPanel.add(operationTable, gbc);
		operationTable.setBorder(BorderFactory
				.createTitledBorder("operation list"));

		gbc.gridx = 3;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.gridheight = 6;
		gbc.weightx = 0.3;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		JScrollPane sp = new JScrollPane(paramTable,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		topPanel.add(sp, gbc);
		sp.setBorder(BorderFactory.createTitledBorder("parameter list"));

		gbc.gridx = 3;
		gbc.gridy = 6;
		gbc.gridwidth = 2;
		gbc.gridheight = 1;
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;
		gbc.fill = GridBagConstraints.NONE;
		topPanel.add(actionButton, gbc);

		gbc.gridx = 5;
		gbc.gridy = 0;
		gbc.gridwidth = 3;
		gbc.gridheight = 7;
		gbc.weightx = 0.4;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		topPanel.add(executionTable, gbc);
		executionTable.setBorder(BorderFactory
				.createTitledBorder("execution list"));

		gbc.gridx = 8;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;
		gbc.fill = GridBagConstraints.NONE;
		topPanel.add(upButton, gbc);

		gbc.gridx = 8;
		gbc.gridy = 5;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;
		gbc.fill = GridBagConstraints.NONE;
		topPanel.add(downButton, gbc);

		gbc.gridx = 8;
		gbc.gridy = 6;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;
		gbc.fill = GridBagConstraints.NONE;
		topPanel.add(removeButton, gbc);

		JPanel bottomPanel = new JPanel();
		bottomPanel.setBorder(BorderFactory
				.createTitledBorder("preview the code"));
		bottomPanel.setLayout(new BorderLayout());
		JPanel tb = new JPanel();
		tb.setLayout(new FlowLayout());
		tb.add(new JLabel("running date:"));

		tb.add(runDateField);
		tb.add(previewButton);
		bottomPanel.add(tb, BorderLayout.NORTH);
		previewCode.setBackground(Color.WHITE);
		bottomPanel.add(new JScrollPane(previewCode), BorderLayout.CENTER);

		setLayout(new BorderLayout());
		add(topPanel, BorderLayout.CENTER);
		add(bottomPanel, BorderLayout.SOUTH);
	}

	public void loadData() {
		loadOperation();
		loadExecution();
	}

	public void loadOperation() {
		OperationDAO opDao = (OperationDAO) Main.context
				.getBean("operationDAO");
		java.util.List<Operation> operationList = opDao.searchAll();
		operationTable.reloadData(operationList);
	}

	public void loadExecution() {
		ExecutionDetailDAO exeDao = (ExecutionDetailDAO) Main.context
				.getBean("executionDetailDAO");
		java.util.List<ExecutionDetail> list = exeDao.searchAll(table_id);
		executionTable.reloadData(list);
	}

	public int getTableId() {
		return table_id;
	}

	public void setTableId(int tabId) {
		table_id = tabId;
		loadExecution();
	}

	@Override
	public String getMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void handle() {
		// TODO Auto-generated method stub

	}

}
